package com.wxy.bank.servlet;

import com.wxy.bank.model.Account;
import com.wxy.bank.service.AccountService;
import com.wxy.bank.service.AccountServiceImpl;
import com.wxy.bank.util.MD5;
import com.wxy.bank.util.StringEncodeUTF8;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

/**
 * 添加一个新的账户
 * @author wxy
 *
 */
@javax.servlet.annotation.WebServlet("/AddAccountServlet")
public class AddAccountServlet extends javax.servlet.http.HttpServlet {
    AccountService accountService = new AccountServiceImpl();
    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //jsp的九个内置对象  session application page pageContext exception out config
        doGet(request,response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //接收网页表单数据：addAccount.jsp
        request.setCharacterEncoding("utf-8");//仅对post有效
        String accountName = request.getParameter("accountName");
        //String accountNameOK = StringEncodeUTF8.encodeUTF8("accountName");
        String accountPsw = request.getParameter("accountPsw");
        String accountPswMd5 = MD5.getMD5(accountPsw);

        String accountNo = request.getParameter("accountNo");
        String bank = request.getParameter("bank");
        Account account = new Account("",accountName,accountPswMd5,accountNo,bank);
        int flag = accountService.addAccount(account);
        //必须设置内容类型并指定编码
        response.setContentType("text/html;charset=UTF-8");
        //设置响应编码，汉字就不乱了，必须放在获得out之前
//      response.setCharacterEncoding("GBK");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        response.sendRedirect("FindAllAccountServlet");
//        if (flag!=0){
//            out.println("成功！");
//        } else {
//            out.println("失败！");
//        }

    }
}
